package com.test;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

public class DSUPanel {
	
	static Logger logger = Logger.getLogger(DSUPanel.class);
	private JPanel panel=null;
	private JPanel children=null;
	private JLabel label=null;
	private File file=null;
	private JTextArea log=null;
	private static final Dimension rigid=new Dimension(1,1);
	private static final float horizontal=Component.LEFT_ALIGNMENT;
	private static final float vertical=Component.TOP_ALIGNMENT;
	private long totalBytes=0l;
	private int MAX_LEVELS=4;
	private int level=0;
	
	public DSUPanel(File file, JTextArea log, int level) {
		if (level<=MAX_LEVELS) {
			this.panel=new JPanel();
			this.panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
			this.panel.setBorder(BorderFactory.createLineBorder(Color.black));
			this.label=new JLabel(file.getName());
			this.label.setAlignmentX(horizontal);
			this.label.setAlignmentY(vertical);
			this.children = new JPanel();
			this.children.setLayout(new BoxLayout(children, BoxLayout.X_AXIS));
			this.children.setAlignmentX(horizontal);
			this.children.setAlignmentY(vertical);
//			this.children.setBackground(Color.red);
			this.panel.add(label);
			this.panel.add(Box.createRigidArea(rigid));
			this.panel.add(children);
			this.panel.add(Box.createRigidArea(rigid));
			this.children.add(Box.createRigidArea(rigid));
			this.panel.setToolTipText("Calculating size...");
		}
		this.file=file;
		this.log=log;
		this.level=level;
		if (file.isDirectory()) logger.debug("Opening directory "+file.getName()+"\n");
		else {
			totalBytes=file.length();
			if (panel!=null) panel.setToolTipText(humanReadableByteCount(totalBytes, false));
			logger.debug("Selecting file "+file.getName()+": "+humanReadableByteCount(totalBytes, false)+"\n");
		}
	}
	
	public JPanel getPanel() {
		return this.panel;
	}
	
	public File getFile() {
		return this.file;
	}
	
	public long length() {
		return totalBytes;
	}
	
//	public DSUPanel addChild(File file) {
//		DSUPanel dsuPanel = new DSUPanel(file, this.log);
//		this.children.add(dsuPanel.getPanel());
//		this.children.add(Box.createRigidArea(rigid));
//		String tot=humanReadableByteCount(totalBytes, false);
//		String fl=humanReadableByteCount(dsuPanel.length(), false);
//		log.append("Adding '"+file.getName()+"' "+fl+" to '"+this.file.getName()+"' size of "+tot+"\n");
//		this.totalBytes=this.totalBytes+dsuPanel.length();
//		panel.setToolTipText(humanReadableByteCount(totalBytes, false));
//		return dsuPanel;
//	}
	
	public void connectChild(DSUPanel dsuPanel) {
		if (this.level>=MAX_LEVELS) return;
		this.children.add(dsuPanel.getPanel());
		this.children.add(Box.createRigidArea(rigid));
		logger.debug("Connected child '"+dsuPanel.getFile().getName()+"' to '"+this.file.getName()+"'\n");
	}
	
	public void addChild(DSUPanel dsuPanel, boolean isLastChild) {
		String tot=humanReadableByteCount(totalBytes, false);
		String fl=humanReadableByteCount(dsuPanel.length(), false);
		logger.debug("Adding '"+dsuPanel.getFile().getName()+"' "+fl+" to '"+this.file.getName()+"' size of "+tot+"\n");
		this.totalBytes=this.totalBytes+dsuPanel.length();
		if (panel!=null && isLastChild) panel.setToolTipText(humanReadableByteCount(totalBytes, false));
	}
	
	public String humanReadableByteCount(long bytes, boolean si) {
	    int unit = si ? 1000 : 1024;
	    if (bytes < unit) return bytes + " B";
	    int exp = (int) (Math.log(bytes) / Math.log(unit));
	    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
	    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
	}
}
